package cn.itsource.ymcc.feign;


import cn.itsource.result.JSONResult;
import cn.itsource.ymcc.domain.MessageEmail;
import cn.itsource.ymcc.domain.MessageSms;
import cn.itsource.ymcc.domain.MessageStation;
import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "service-common",fallbackFactory = CommonFeignClientFallbackFactory.class)
public interface CommonFeignClient {
    @RequestMapping(value="/messageEmail/save",method= RequestMethod.POST)
    JSONResult saveOrUpdate(@RequestBody MessageEmail messageEmail);

    @RequestMapping(value="/messageSms/save",method= RequestMethod.POST)
    JSONResult saveOrUpdate(@RequestBody MessageSms messageSms);

    @RequestMapping(value="/messageStation/save",method= RequestMethod.POST)
    JSONResult saveOrUpdate(@RequestBody MessageStation messageStation);

}

@Component
class CommonFeignClientFallbackFactory implements FallbackFactory<CommonFeignClient> {

    @Override
    public CommonFeignClient create(Throwable throwable) {
        return new CommonFeignClient() {
            @Override
            public JSONResult saveOrUpdate(MessageEmail messageEmail) {
                throwable.printStackTrace();
                return JSONResult.error("信息保存失败！");
            }

            @Override
            public JSONResult saveOrUpdate(MessageSms messageSms) {
                throwable.printStackTrace();
                return JSONResult.error("信息保存失败！");
            }

            @Override
            public JSONResult saveOrUpdate(MessageStation messageStation) {
                throwable.printStackTrace();
                return JSONResult.error("信息保存失败！");
            }
        };
    }
}

